<?php

namespace App\Http\Controllers\Home;

use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use ErrorException;

class MessageController extends CommonController
{
    //限制同ip当天留言次数
    protected $time = 5;

    //在线留言
    public function index(Request $request, BaseServer $baseServer)
    {
        if (config('area')) {
            $data['title'] = config('app.message_name') . '_' . config('set.web_name') . '_' . config('range')[config('area')] . '分站';
        } else {
            $data['title'] = config('app.message_name') . '_' . config('set.web_name');
        }

        $data['keywords'] = config('set.web_keywords');
        $baseServer->keywordDowork($request, $data['keywords']);

        $data['description'] = config('set.web_description');

        $data['frames'] = $baseServer->frame([
            ['url' => route('message'), 'name' => config('app.message_name') . ''],
        ]);

        if (!empty($request->info)) {
            $data['info'] = \App\Info::find($request->info);
        }

        if ((bool)config('set.wap_jump') && is_mobile()) {
            return view('home.m.' . config('set.wap_theme') . '.message.message', $data);
        }

        return view('home.pc.message.message', $data);
    }

    //在线留言处理
    public function message(Request $request, BaseServer $baseServer)
    {
        //验证留言次数
        $time = \App\Message::where('ip', getIp())
            ->where('created_at', '>', date('Y-m-d 00:00:00'))
            ->where('created_at', '<', date('Y-m-d 23:59:59'))
            ->count();
        if ($this->time <= $time) {
            $message['message'] = message(false, '留言/评论失败', '留言/评论次数过多,请明天再来吧~');
            return back()->with($message);
        }

        $messageFields = \App\MessageField::where('status', 1)
            ->select('alias', 'field', 'verify')
            ->get()
            ->toArray();
        foreach ($messageFields as $field) {
            $alias = $field['alias'];
            $validate[$alias] = $field['verify'];
            $create[$field['field']] = $request->$alias;
        }
        $create['status'] = 1;
        $create['info'] = $request->info;
        $create['ip'] = getIp();
        $this->validate($request, $validate);

        $result = \App\Message::create($create);
        if ($result) {
            if (!empty(config('app.wechatSet.appid'))
                && !empty(config('app.wechatSet.secret'))
                && count(config('app.wechatSet.openid')) > 0
                && count(config('app.wechatSet.template_id')) > 0) {
                try {
                    $baseServer->sendWechatMsg($request);
                } catch (ErrorException $e) {
                    //预留发送到oa系统需要添加白名单的接口
                    //dd($request->ip());
                }
            }
            $message['message'] = message(true, config('app.message_success_name'), config('app.message_success_explore_name'));
        } else {
            $message['message'] = message(false, config('app.message_false_name'), config('app.message_false_explore_name'));
        }

        return back()->with($message);
    }
}
